package top.jx.service;

import jcifs.Config;
import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import jcifs.smb.SmbSession;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.jx.dao.FileInfoDao;
import top.jx.file.FileService;
import top.jx.model.FileInfo;
import top.jx.file.FileUtil;
import top.jx.util.PrintTimeUtils;

import java.io.*;
import java.util.UUID;

@Service
public class SambaService {

    @Value(value = "${samba.ip}")
    private String ip;  // 替换成真实的 ip
    @Value(value = "${samba.port}")
    private int port;
    @Value(value = "${samba.rootPath}")
    private String rootPath;
    @Value(value = "${samba.user}")
    private String user;
    @Value(value = "${samba.passwd}")
    private String passwd;

    @Autowired
    private FileInfoDao fileInfoDao;
    @Autowired
    private FileService fileService;



    public FileInfo upload(File file, String fileName, String type) {
        System.setProperty("jcifs.smb.client.dfs.disabled", "true");
        System.setProperty("jcifs.netbios.wins", "172.30.100.230");
        Config.setProperty("jcifs.smb.client.dfs.disabled", "true");
        Config.setProperty("jcifs.netbios.wins", "172.30.100.230");
        FileInfo fileInfo = new FileInfo();
        try {
            String uuid = String.valueOf(UUID.randomUUID()).toUpperCase();
            String folderPath = uuid.substring(0, 2);
            String path = "smb://" + ip + rootPath + "/" + folderPath + "/";

            UniAddress ua = UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, passwd);
            SmbSession.logon(ua, port, auth);
            SmbFile folder = new SmbFile(path, auth);
            if(!folder.exists()){
                folder.mkdirs();
            }

            SmbFile sf = new SmbFile(path+fileName, auth);
            long l = System.currentTimeMillis();

//            IOUtils.copyLarge(new FileInputStream(file), sf.getOutputStream());




//
            InputStream in = null;
            OutputStream out = null;
            try{

                //打开一个文件输入流执行本地文件，要从它读取内容
                in = new BufferedInputStream(new FileInputStream(file));

                //打开一个远程Samba文件输出流，作为复制到的目的地
                out = new BufferedOutputStream(new SmbFileOutputStream(sf));

                int bufferSize = 1024;

                //采用高效输入流获取网络上的资源
                byte[] buffer = new byte[bufferSize];


//                int len = 0;
//                ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                while ((len = in.read(buffer)) != -1) {
//                    bos.write(buffer, 0, len);
//                }
//                bos.close();



                int i =0;
                int count = -1;
                while((count = in.read(buffer,0, bufferSize)) != -1) {
                    out.write(buffer, 0, count);
                }

//                System.out.println("111222212");
//                    out.write(bos.toByteArray());

            } catch  (Exception e) {
                e.printStackTrace();

            } finally  {
                try  {
                    out.close();
                    in.close();
                } catch  (IOException e) {
                    e.printStackTrace();
                }
            }
            PrintTimeUtils.printTime(l);





            fileInfo.setPath(folderPath + "/");
            fileInfo.setName(file.getName());
            fileInfo.setSuffix(FileUtil.getFileSuffix(file));
            fileInfo.setSize(FileUtil.getFileSize(file));
            fileInfo.setType(type);
            fileInfoDao.save(fileInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileInfo;
    }


    public File getFile(String path) {
        File tempFile = fileService.getTempFile(FileUtil.getFileSuffix(new File(path)));
        try {
            UniAddress ua = UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, passwd);
            SmbSession.logon(ua, port, auth);
            SmbFile sf = new SmbFile("smb://" + ip + rootPath + "/" + path, auth);

            IOUtils.copyLarge(sf.getInputStream(), new FileOutputStream(tempFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempFile;
    }



    public void testPut() {
        try {
            UniAddress ua = UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, passwd);
            SmbSession.logon(ua, port, auth);
            SmbFile sf = new SmbFile("smb://" + ip + "/portrait/upload.txt", auth);
            IOUtils.copyLarge(new FileInputStream("f:/upload.txt"), sf.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testGet() {
        try {
            UniAddress ua = UniAddress.getByName(ip);
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, user, passwd);
            SmbSession.logon(ua, port, auth);
            SmbFile sf = new SmbFile("smb://" + ip + "/files/fileDown/00/005BBBE7-D2F7-4D69-815F-4425D53DADCF.png", auth);
            IOUtils.copyLarge(sf.getInputStream(), new FileOutputStream("d:/005BBBE7-D2F7-4D69-815F-4425D53DADCF.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SambaService smbaService = new SambaService();
        smbaService.testGet();

    }

}
